<template>
  <mescroll-uni
    ref="mescrollRef"
    :down="downOption"
    :height="height"
    :up="upOption"
    top="80"
    @down="downCallback"
    @emptyclick="emptyClick"
    @init="mescrollInit"
    @up="upCallback"
  >
    <good-list
      :list="goods"
      :option="option"
      :seeImageList="seeImageList"
      :titleName="tabs[i].name"
    ></good-list>
  </mescroll-uni>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex'
import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js'
import MescrollMoreItemMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-more-item.js'
import goodList from './goodList.vue'

export default {
  mixins: [MescrollMixin, MescrollMoreItemMixin],
  computed: { ...mapGetters(['userInfo', 'api']) },
  components: {
    goodList
  },
  data() {
    return {
      downOption: {
        auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
      },
      upOption: {
        auto: false, // 不自动加载
        noMoreSize: 4, //如果列	表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
        empty: {
          tip: `~ ${this.$t('暂无记录')} ~`,
          fixed: true,
          top: '200rpx'
        },
        textNoMore: `-- ${this.$t('触底了')} --`
      },
      goods: [], //列表数据
      seeImageList: [] //用印记录里的所有图片，方便下一张
    }
  },
  props: {
    i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
    index: {
      // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
      type: Number,
      default() {
        return 0
      }
    },
    tabs: {
      type: Array,
      default() {
        return []
      }
    },
    height: [Number, String], // mescroll的高度
    option: {
      //当前页面信息，从哪里点进的
      type: Object,
      default() {
        return {}
      }
    }
  },
  mounted() {
    //刷新
    uni.$on('applicationDetailsToApplicationList', () => {
      this.mescroll.triggerDownScroll()
    })
  },
  methods: {
    /*下拉刷新的回调 */
    downCallback() {
      this.goods = []
      // 这里加载你想下拉刷新的数据, 比如刷新轮播数据
      // loadSwiper();
      // 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
      this.mescroll.resetUpScroll()
    },
    /*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
    upCallback(page) {
      //联网加载数据
      let type = this.tabs[this.i].name
      if (type === this.$t('用印详情')) {
        this.PrintingDetails()
        this.mescroll.lockUpScroll(true)
      } else if (type === this.$t('审批流程')) {
        this.timeAxisData()
        this.mescroll.lockUpScroll(true)
      } else if (type === this.$t('用印记录')) {
        //electronicApplication为true代表电子章的盖章记录
        if (this.option.clickItem.electronicApplication === true) {
          this.mescroll.endErr()
        } else {
          this.PrintingRecord(page)
        }
      }
    },
    //用印记录
    PrintingRecord(page) {
      //console.log(this.option);
      let req = {
        applicationId: this.option.clickItem.id,
        pageSize: page.size, //每页的数量
        pageNum: page.num, // 当前显示第几页
        page: true
      }
      //章的接口
      this.$u
        .post(this.api.GLOBAL_sealRecordInfo_getVoByApplicationId, req)
        .then((res) => {
          if (page.num === 1) {
            this.goods = []
          }
          if (res.code === 200 && res.data && res.data.list) {
            this.mescroll.endBySize(res.data.list.length, res.data.total)
            //如果是量子图片默认显示水印图片
            this.setImageWatermark(res.data.list)
            this.goods = this.goods.concat(res.data.list)
            //判断是否只有点击时才解密
            if (!getApp().globalData.GK_QSS_ONOFF.clickDecrypt) {
              //计算量子解密
              this.gdQssFn(res.data.list)
            }
          } else {
            this.mescroll.endBySize(0, 0)
            this.goods = []
          }
          //把列表里的所有图片放入到数组中方便下一张
          this.pushforEachImage()
        })
        .catch((err) => {
          //联网失败, 结束加载
          this.mescroll.endErr()
        })
    },
    //如果是量子图片默认显示水印图片
    setImageWatermark(list) {
      list.forEach((item) => {
        if (item.useUrls) {
          item.useUrls.forEach((res) => {
            if (res.enType === 3 || res.enType === 4) {
              res.QssfileUrl = res.fileUrl
              res.fileUrl = '../../../static/dianxin/shuiyin2.png'
            }
          })
        }
        if (item.temporaryUrls) {
          item.temporaryUrls.forEach((res) => {
            if (res.enType === 3 || res.enType === 4) {
              res.QssfileUrl = res.fileUrl
              res.fileUrl = '../../../static/dianxin/shuiyin2.png'
            }
          })
        }
        if (item.passwordInfos) {
          item.passwordInfos.forEach((res) => {
            if (res.enType === 3 || res.enType === 4) {
              res.QssfileUrl = res.fileUrl
              res.fileUrl = '../../../static/dianxin/shuiyin2.png'
            }
          })
        }
        if (item.auditorUrls) {
          item.auditorUrls.forEach((res) => {
            if (res.enType === 3 || res.enType === 4) {
              res.QssfileUrl = res.fileUrl
              res.fileUrl = '../../../static/dianxin/shuiyin2.png'
            }
          })
        }
        if (item.replenishList) {
          item.replenishList.forEach((res) => {
            if (res.enType === 3 || res.enType === 4) {
              res.QssfileUrl = res.fileUrl
              res.fileUrl = '../../../static/dianxin/shuiyin2.png'
            }
          })
        }
        if (item.warnUrls) {
          item.warnUrls.forEach((res) => {
            if (res.enType === 3 || res.enType === 4) {
              res.QssfileUrl = res.fileUrl
              res.fileUrl = '../../../static/dianxin/shuiyin2.png'
            }
          })
        }
        if (item.overTimesUrls) {
          item.overTimesUrls.forEach((res) => {
            if (res.enType === 3 || res.enType === 4) {
              res.QssfileUrl = res.fileUrl
              res.fileUrl = '../../../static/dianxin/shuiyin2.png'
            }
          })
        }
      })
    },
    //把列表里的所有图片放入到数组中方便下一张
    pushforEachImage() {
      let arr = []
      this.goods.forEach((item) => {
        if (item.useUrls) {
          let list = item.useUrls.map((res) => {
            return {
              ...res,
              ...item,
              useUrls: '',
              title: this.$t('盖章图片')
            }
          })
          arr = arr.concat(list)
        }
        if (item.temporaryUrls) {
          let list = item.temporaryUrls.map((res) => {
            return {
              ...res,
              ...item,
              temporaryUrls: '',
              title: this.$t('临时照片')
            }
          })
          arr = arr.concat(list)
        }
        if (item.passwordInfos) {
          let list = item.passwordInfos.map((res) => {
            return {
              ...res,
              ...item,
              passwordInfos: '',
              title: this.$t('密码图片')
            }
          })
          arr = arr.concat(list)
        }
        if (item.auditorUrls) {
          let list = item.auditorUrls.map((res) => {
            return {
              ...res,
              ...item,
              auditorUrls: '',
              title: this.$t('审计图片')
            }
          })
          arr = arr.concat(list)
        }
        if (item.replenishList) {
          let list = item.replenishList.map((res) => {
            return {
              ...res,
              ...item,
              replenishList: '',
              title: this.$t('追加图片')
            }
          })
          arr = arr.concat(list)
        }
        if (item.warnUrls) {
          let list = item.warnUrls.map((res) => {
            return {
              ...res,
              ...item,
              warnUrls: '',
              title: this.$t('按压超时')
            }
          })
          arr = arr.concat(list)
        }
        if (item.overTimesUrls) {
          let list = item.overTimesUrls.map((res) => {
            return {
              ...res,
              ...item,
              overTimesUrls: '',
              title: this.$t('超次图片')
            }
          })
          arr = arr.concat(list)
        }
      })
      this.seeImageList = arr
    },
    //计算量子解密
    async gdQssFn(list) {
      for (let b = 0; b < list.length; b++) {
        if (list[b].useInfos) {
          //盖章图片
          for (let a = 0; a < list[b].useInfos.length; a++) {
            if (list[b].useInfos[a].secretKey && !list[b].useInfos[a].QSS) {
              list[b].useInfos[a].QSS = true //此图片为量子图片显示量子解密动画
              //#ifdef APP-PLUS
              let base64 = await this.isgkQSS(list[b].useInfos[a], a)
              if (base64) {
                list[b].useInfos[a].QSS = false
                this.$set(list[b].useInfos[a], 'fileUrl', base64)
              }
              //#endif
            }
          }
        }
        if (list[b].temporaryInfos) {
          //临时图片
          for (let a = 0; a < list[b].temporaryInfos.length; a++) {
            if (list[b].temporaryInfos[a].secretKey && !list[b].temporaryInfos[a].QSS) {
              list[b].temporaryInfos[a].QSS = true //此图片为量子图片显示量子解密动画
              //#ifdef APP-PLUS
              let base64 = await this.isgkQSS(list[b].temporaryInfos[a], a)
              if (base64) {
                list[b].temporaryInfos[a].QSS = false
                this.$set(list[b].temporaryInfos[a], 'fileUrl', base64)
              }
              //#endif
            }
          }
        }
        if (list[b].auditorInfos) {
          //审计图片
          for (let a = 0; a < list[b].auditorInfos.length; a++) {
            if (list[b].auditorInfos[a].secretKey && !list[b].auditorInfos[a].QSS) {
              list[b].auditorInfos[a].QSS = true //此图片为量子图片显示量子解密动画
              //#ifdef APP-PLUS
              let base64 = await this.isgkQSS(list[b].auditorInfos[a], a)
              if (base64) {
                list[b].auditorInfos[a].QSS = false
                this.$set(list[b].auditorInfos[a], 'fileUrl', base64)
              }
              //#endif
            }
          }
        }
        if (list[b].numoutInfos) {
          //超次图片
          for (let a = 0; a < list[b].numoutInfos.length; a++) {
            if (list[b].numoutInfos[a].secretKey && !list[b].numoutInfos[a].QSS) {
              list[b].numoutInfos[a].QSS = true //此图片为量子图片显示量子解密动画
              //#ifdef APP-PLUS
              let base64 = await this.isgkQSS(list[b].numoutInfos[a], a)
              if (base64) {
                list[b].numoutInfos[a].QSS = false
                this.$set(list[b].numoutInfos[a], 'fileUrl', base64)
              }
              //#endif
            }
          }
        }
        if (list[b].timeoutInfos) {
          //按压超时
          for (let a = 0; a < list[b].timeoutInfos.length; a++) {
            if (list[b].timeoutInfos[a].secretKey && !list[b].timeoutInfos[a].QSS) {
              list[b].timeoutInfos[a].QSS = true //此图片为量子图片显示量子解密动画
              //#ifdef APP-PLUS
              let base64 = await this.isgkQSS(list[b].timeoutInfos[a], a)
              if (base64) {
                list[b].timeoutInfos[a].QSS = false
                this.$set(list[b].timeoutInfos[a], 'fileUrl', base64)
              }
              //#endif
            }
          }
        }
        if (list[b].passwordInfos) {
          //密码模式
          for (let a = 0; a < list[b].passwordInfos.length; a++) {
            if (list[b].passwordInfos[a].secretKey && !list[b].passwordInfos[a].QSS) {
              list[b].passwordInfos[a].QSS = true //此图片为量子图片显示量子解密动画
              //#ifdef APP-PLUS
              let base64 = await this.isgkQSS(list[b].passwordInfos[a], a)
              if (base64) {
                list[b].passwordInfos[a].QSS = false
                this.$set(list[b].passwordInfos[a], 'fileUrl', base64)
              }
              //#endif
            }
          }
        }
      }
    },
    //是否进行国科量子解密
    async isgkQSS(data, index) {
      let base64 = await this.$lizhao.QSS.SM4DecryptBigData(data.fileUrl, data.secretKey)
      return base64
    },

    //审批流程
    timeAxisData() {
      let req = {
        applicationId: this.option.clickItem.id
      }
      //章的接口
      this.$u
        .post(this.api.GLOBAL_applicationDealLog_getByApplication, req)
        .then((res) => {
          if (res.code === 200 && res.data) {
            this.mescroll.endDownScroll()
            this.goods = res.data
          } else {
            this.mescroll.endBySize(0, 0)
          }
        })
        .catch((err) => {
          //联网失败, 结束加载
          this.mescroll.endErr()
        })
    },
    //用印详情
    PrintingDetails() {
      let req = {
        applicationId: this.option.clickItem.id
      }
      //章的接口
      this.$u
        .post(this.api.GLOBAL_application_getAuditorApplicationById, req)
        .then((res) => {
          if (res.code === 200 && res.data) {
            this.mescroll.endDownScroll()
            this.goods = res.data
            //区分图片和附件
            let imageList = [],
              fileList = []
            this.goods.fileEntities.map((item) => {
              if (item.type === 0) {
                imageList.push(item)
              } else {
                fileList.push(item)
              }
            })
            this.goods.imageList = imageList
            this.goods.fileList = fileList
          } else {
            this.mescroll.endBySize(0, 0)
          }
        })
        .catch((err) => {
          //联网失败, 结束加载
          this.mescroll.endErr()
        })
    },
    //授权
    authoriza(page, type) {
      let req = {
        pageSize: page.size, //每页的数量
        pageNum: page.num, // 当前显示第几页
        page: true
      }
      let url =
        type === '待授权'
          ? this.api.GLOBAL_application_getUnAuthorizedByOwn
          : this.api.GLOBAL_application_getAuthorizedByOwn
      //章的接口
      this.$u
        .get(url, req)
        .then((res) => {
          if (res.code === 200 && res.data && res.data.list) {
            this.mescroll.endBySize(res.data.list.length, res.data.total)
            this.goods = this.goods.concat(res.data.list)
          } else {
            this.mescroll.endBySize(0, 0)
            this.goods = []
          }
        })
        .catch((err) => {
          //联网失败, 结束加载
          this.mescroll.endErr()
        })
    },
    //审计
    audit(page, type) {
      let req = {
        pageSize: page.size, //每页的数量
        pageNum: page.num, // 当前显示第几页
        page: true
      }
      let url =
        type === '待审计'
          ? this.api.GLOBAL_application_getNotAuditedByOwn
          : this.api.GLOBAL_application_getAuditedByOwn
      //章的接口
      this.$u
        .get(url, req)
        .then((res) => {
          if (res.code === 200 && res.data && res.data.list) {
            this.mescroll.endBySize(res.data.list.length, res.data.total)
            this.goods = this.goods.concat(res.data.list)
          } else {
            this.mescroll.endBySize(0, 0)
            this.goods = []
          }
        })
        .catch((err) => {
          //联网失败, 结束加载
          this.mescroll.endErr()
        })
    },

    //点击空布局按钮的回调
    emptyClick() {}
  }
}
</script>
